﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Composite.Data;
using System.Web.Script.Serialization;
using LCA.Common.Extensions;

public partial class Admin_LCASystem_Controls_TransportationA : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void RefreshComponents(Guid? SystemId)
    {
        ctlTransportAModal.RefreshComponents();

        // Load Input into js variables
        using (DataConnection conn = new DataConnection())
        {

            if (!SystemId.HasValue)
                return;

            var list = from ta in conn.Get<LCA.Data.LCASystemTransportA>()
                       join f in conn.Get<LCA.Data.Flow>() on ta.Flow equals f.Id
                       join u in conn.Get<LCA.Data.Unit>() on ta.Unit equals u.Id into joined
                       from j in joined.DefaultIfEmpty()
                       where ta.RelatedSystem == SystemId.Value
                       orderby f.Name
                       select new
                       {
                           item_id = ta.Flow,
                           item_type ="Flow",
                           item_name = f.Name,
                           amount = ta.Amount,
                           unit_id = ta.Unit,
                           unit_name = j.Name,
                       };

            txtTransportA.Text = new JavaScriptSerializer().Serialize(list.ToList());

        }


    }

    public void SaveData(Guid SystemId)
    {
        
        var table = new JavaScriptSerializer().Deserialize<dynamic>(txtTransportA.Text);

        using (DataConnection connection = new DataConnection())
        {
            
            ////////////////////////////// Output
            var deletedOutputList = from i in connection.Get<LCA.Data.LCASystemTransportA>()
                                    where i.RelatedSystem == SystemId
                                    select i;

            foreach (LCA.Data.LCASystemTransportA item in deletedOutputList)
            {
                connection.Delete(item);
            }

            if (table != null)
            {
                LCA.Data.LCASystemTransportA ta = null;
                foreach (var row in table)
                {
                    string item_id = row["item_id"].ToString();
                    string item_type = row["item_type"].ToString();

                    string amount = row["amount"].ToString();
                    string unit_id = row["unit_id"];

                    ta = connection.CreateNew<LCA.Data.LCASystemTransportA>();

                    ta.Flow = item_id.ToGuid().Value;
                    ta.Amount = amount.ToString();
                    ta.Unit = unit_id.ToGuid();
                    ta.RelatedSystem = SystemId;

                    connection.Add(ta);
                }
            }
            
        }

    }
}